package com.example.demo.vo;

import com.example.demo.entity.ArticleEntity;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.text.SimpleDateFormat;

/**
 * 文章列表行VO
 */
@Data
public class ArticleListVO {
    /**
     * 日期显示格式
     */
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy年M月d日");
    /**
     * ID
     */
    private Integer id;
    /**
     * 文章标题
     */
    private String title;
    /**
     * 文章具体内容
     */
    private String content;
    /**
     * 发表时间
     */
    private String created;
    /**
     * '文章分类'
     */
    private String categories;

    /**
     * 将Entity转换成VO
     *
     * @param entity
     * @return
     */
    public static ArticleListVO convert(ArticleEntity entity) {
        ArticleListVO vo = new ArticleListVO();
        BeanUtils.copyProperties(entity, vo, "created");
        if (entity.getCreated() != null) {
            vo.setCreated(sdf.format(entity.getCreated()));
        }
        return vo;
    }
}
